<template>
	<om-page :title="pageTitle">
		<view style="z-index: 0;">
			<view v-show="currentTab === 0">
				<view class="sticky-item" style="position: fixed; z-index: 9; height: 75px; width: 100%;">
					<om-topsearch :useThemeColor="false" :maxlength="100" placeholder="请输入" @confirm="searchClick"
						@input="searchClick"></om-topsearch>
					<view style="background: #FFF;">已加载 {{ tab0.datas.length }} 条数据</view>
				</view>
				<view style="padding-top: 75px;">
					<uni-list>
						<view v-for="(item, index) in tab0.datas" :key="index">
							<uni-list-item :title="item.orderNum1" :avatar="index + 1"
								:avatarBgColor="calcBgColor(item)" @click="infoTab(item)">
								<view slot="note">
									<view v-if="item.workPeriod">工期：<text class="">{{item.workPeriod}}天</text></view>
									<view v-if="item.planStart1">计划开始日期：<text class="">{{item.planStart1}}</text></view>
									<view>计划完成日期：<text class="">{{item.planEnd1}}</text></view>
									<view v-if="item.actualCompleteDate1">实际完成日期：<text
											class="">{{item.actualCompleteDate1}}</text></view>
									<view v-if="item.reportDate1">报告日期：<text class="">{{item.reportDate1}}</text></view>
									<view>交付物：<text class="">{{item.deliveryName}}</text></view>
									<view v-if="item.dutyUserName">负责人：<text class="">{{item.dutyUserName}}</text>
									</view>
									<view v-if="item.evaluateUserName">评价人：<text
											class="">{{item.evaluateUserName}}</text></view>
									<view class='cu-tag sm radius light bg-orange'
										style="background: #FFF; padding: 0;font-size: 12px">{{getTag1(item)}}</view>
									<view v-show="getClickBtn(item).length>0" style="text-align: right;">
										<view v-for="sitem in getClickBtn(item)"
											style="display: inline-block;margin: 0 5px;">
											<view class='cu-tag sm radius light bg-blue'
												@click.stop="getoLink(sitem.link)"
												style=" width: 64px; height: 32px;font-size: 12px">{{ sitem.text }}
											</view>
										</view>
									</view>
								</view>
							</uni-list-item>
						</view>
					</uni-list>
				</view>
				<!-- <om-workpage :scrollTop="tab0.scrollTop" :forAll="false" :useThemeColor="false"  @confirm="searchClick" :datas="tab0.datas">
					  <template slot="header">
						  <view>已加载 {{ tab0.datas.length }} 条数据</view>
					  </template>
					  <view slot="content">
						  <uni-list>
						  	<view v-for="(item, index) in tab0.datas" :key="index">
						  		<uni-list-item
						  			:title="item.orderNum1"
						  			:avatar="index + 1"
						  			:avatarBgColor="calcBgColor(item)"
						  			@click="infoTab(item)"
						  		>
						  		 <view slot="note">
						  			<view>计划完成日期：<text class="">{{item.planEnd1}}</text></view>
									<view v-if="item.reportDate1">报告日期：<text class="">{{item.reportDate1}}</text></view>
									<view v-if="item.evaluateUserName">评价人：<text class="">{{item.evaluateUserName}}</text></view>
									<view class='cu-tag sm radius light' :class="item.reportDate1?'bg-mauve':'bg-orange'">{{getTag1(item)}}</view>
						  		 </view>
						  		</uni-list-item>
						  	</view>
						  </uni-list>
					  </view>
				</om-workpage> -->
				<tui-loadmore :visible="tab0.loadding"></tui-loadmore>
				<tui-nomore :visible="!tab0.pullUpOn"></tui-nomore>
				<uni-fab horizontal="right" @trigger="trigger" direction="vertical" :content="buttons"
					:scrollTop="tab0.scrollTop" />
				<!-- <om-button :buttons="tab0.toolbar" @btnChange="btnChange0" fontSize="medium"></om-button> -->
			</view>
			<!-- <view v-show="currentTab === 1">
				<om-workpage :scrollTop="tab1.scrollTop"  :forAll="false" :useThemeColor="false"  @confirm="searchClick" :datas="tab1.datas">
					<template slot="header">
						<view>已加载 {{ tab1.datas.length }} 条数据</view>
					</template>
					<view slot="content">
					  <uni-list>
						<view v-for="(item, index) in tab1.datas" :key="index">
							<uni-list-item
								:title="item.orderNum1"
								:avatar="index + 1"
								:avatarBgColor="calcBgColor(item)"
								@click="infoTab(item)"
							>
							 <view slot="note">
								 <view>计划完成日期：<text class="">{{item.planEnd1}}</text></view>
								<view v-if="item.reportDate1">报告日期：<text class="">{{item.reportDate1}}</text></view>
								<view v-if="item.dutyUserName">负责人：<text class="">{{item.dutyUserName}}</text></view>
								<view class='cu-tag sm radius light' :class="(item.reportDate1 || item.flowStatus)?'bg-mauve':'bg-orange'">{{getTag2(item)}}</view>
							 </view>
							</uni-list-item>
						</view>
					  </uni-list>
					</view>
				</om-workpage>
				<tui-loadmore :visible="tab1.loadding"></tui-loadmore>
				<tui-nomore :visible="!tab1.pullUpOn"></tui-nomore>
					<uni-fab horizontal="right" @trigger="trigger" direction="vertical" :content="buttons" :scrollTop="tab1.scrollTop" />
					<om-button :buttons="tab1.toolbar"  @btnChange="btnChange1" fontSize="medium"></om-button>
			</view> -->
		</view>
	</om-page>
</template>

<script>
	import {
		mapState
	} from 'pinia';
	import {
		useLoginUserStore
	} from '@/stores/login-user.js'
	const app = getApp();
	export default {
		data() {
			return {
				tab0: {
					searchStr: '',
					datas: [],
					total: 0,
					page: 0,
					scrollTop: 0,
					loadding: false,
					pullUpOn: true,
					toolbar: [{
						color: 'white',
						bgcolor: 'bg-steelblue',
						text: '去报告'
					}, {
						color: 'white',
						bgcolor: 'bg-steelblue',
						text: '去调整'
					}]
				},
				tab1: {
					searchStr: '',
					datas: [],
					total: 0,
					page: 0,
					scrollTop: 0,
					loadding: false,
					pullUpOn: true,
					toolbar: [{
						color: 'white',
						bgcolor: 'bg-steelblue',
						text: '去评价'
					}]
				},
				currentTab: 0,
				buttons: [app.globalData.button.home],
				pageTitle: "",
			};
		},
		computed: {
			...mapState(useLoginUserStore, ['userId', 'userName'])
		},
		created: function() {
			this.searchClick = this._.debounce(this.searchClick, 500)
		},
		methods: {
			getoLink(link) {
				uni.navigateTo({
					url: link
				})
			},
			getClickBtn(item) {
				let arr = [];
				let planDataStr = new Date(item.planEnd1).getTime()
				var myDate = new Date()
				var myDateYear = myDate.getFullYear()
				var myDateMonth = (myDate.getMonth() + 1) < 10 ? '0' + (myDate.getMonth() + 1) : (myDate.getMonth() + 1)
				var myDateDay = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate()
				var nowDate = myDateYear + '-' + myDateMonth + '-' + myDateDay
				let nowDataStr = new Date(nowDate).getTime()

				if (item.remindFlag) {
					arr.push({
						text: '去调整',
						link: '/pages/form/obj-work-monthlyworkadjustapply/obj-work-monthlyworkadjustapply?title=工作计划调整&code=OBJ0510&itemid=' +
							item.id
					})
				} else {
					if (item.flowStatus) {

					} else {
						if (item.reportDate1) {
							if (item.evaluateUserName == this.userName && !item.inAdjust) {
								arr.push({
									text: '去评价',
									link: '/pages/form/pm-work-eva/pm-work-eva?title=工作项评价&code=PER0302&itemid=' +
										item.id
								})
							}
						} else {
							if (item.evaluateUserName == this.userName && nowDataStr >= planDataStr && !item.inAdjust) {
								arr.push({
									text: '去评价',
									link: '/pages/form/pm-work-eva/pm-work-eva?title=工作项评价&code=PER0302&itemid=' +
										item.id
								})
							}
							if (item.dutyUserName == this.userName) {
								if (item.planStart1 && nowDataStr >= new Date(item.planStart1).getTime()) {
									arr.push({
										text: '去报告',
										link: '/pages/form/pm-work-report/pm-work-report?title=工作完成报告&code=PER0301&itemid=' +
											item.id
									})
									arr.push({
										text: '去调整',
										link: '/pages/form/obj-work-monthlyworkadjustapply/obj-work-monthlyworkadjustapply?title=工作计划调整&code=OBJ0510&itemid=' +
											item.id
									})
								}
								if (!item.planStart1) {
									arr.push({
										text: '去报告',
										link: '/pages/form/pm-work-report/pm-work-report?title=工作完成报告&code=PER0301&itemid=' +
											item.id
									})
									arr.push({
										text: '去调整',
										link: '/pages/form/obj-work-monthlyworkadjustapply/obj-work-monthlyworkadjustapply?title=工作计划调整&code=OBJ0510&itemid=' +
											item.id
									})
								}
							}
						}
					}
				}

				return arr
			},
			getTag1(item) {
				var tip = ''
				if (item.remindFlag) {

				} else if (item.inAdjust) {
					tip = '调整中'
				} else {
					if (item.flowStatus) {
						// tip += item.flowStatus
						if (item.inEvaluate) {
							tip += ('已评价' + item.flowStatus)
						}
						if (item.inReport) {
							tip += ('已报告' + item.flowStatus)
						}
					} else {
						tip = item.reportDate1 ? '已报告未评价' : '未报告'
					}
				}

				return item.executorName ? tip + "(" + item.executorName + ")" : tip;
			},
			getTag2(item) {
				var tip = ''
				if (item.inAdjust) {
					tip = '调整中'
				} else {
					if (item.flowStatus) {
						tip += ('已评价' + item.flowStatus)
					} else {
						tip = item.reportDate1 ? '未评价' : '未报告'
					}
				}
				return item.executorName ? tip + "(" + item.executorName + ")" : tip;
			},
			calcBgColor(item) {
				return this.ColorList[3].name
			},
			trigger(e) {
				if (e.item.text === '返回首页') {
					uni.switchTab({
						url: '/pages/main/main/main'
					});
				} else if (e.item.text === '返回顶部') {
					this['tab' + this.currentTab].scrollTop = 0;
					uni.pageScrollTo({
						scrollTop: 0
					});
				}
			},
			searchClick(e) {
				let _that = this;
				_that['tab' + this.currentTab].searchStr = e.value;
				_that.reloadData();
			},
			reloadData: function() {
				let _that = this;
				_that['tab' + this.currentTab].total = 0;
				_that['tab' + this.currentTab].datas = [];
				_that['tab' + this.currentTab].scrollTop = 0;
				_that['tab' + this.currentTab].page = 0;
				_that['tab' + this.currentTab].loadding = false;
				_that['tab' + this.currentTab].pullUpOn = true;
				let func = '';
				if (_that.currentTab == 0) {
					func = 'Launch';
				} else {
					func = 'Pending';
				}
				return _that.loadData(func, _that.currentTab);
			},
			infoTab(item) {
				if (item.showColumns) {
					let title = ""
					if (item.workName.length > 10) {
						title = item.workName.substr(0, 10) + '...';
					} else {
						title = item.workName
					}
					var buttons = [];

					let planDataStr = new Date(item.planEnd1).getTime()
					var myDate = new Date()
					var myDateYear = myDate.getFullYear()
					var myDateMonth = (myDate.getMonth() + 1) < 10 ? '0' + (myDate.getMonth() + 1) : (myDate.getMonth() +
						1)
					var myDateDay = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate()
					var nowDate = myDateYear + '-' + myDateMonth + '-' + myDateDay
					let nowDataStr = new Date(nowDate).getTime()

					if (item.flowStatus) {

					} else {
						if (item.reportDate1) {
							if (item.evaluateUserName == this.userName && !item.inAdjust) {
								buttons.push({
									color: 'white',
									bgcolor: 'bg-steelblue',
									text: '去评价',
									link: '/pages/form/pm-work-eva/pm-work-eva?title=工作项评价&code=PER0302&itemid=' +
										item.id
								})
							}
						} else {
							if (item.evaluateUserName == this.userName && nowDataStr >= planDataStr && !item.inAdjust) {
								buttons.push({
									color: 'white',
									bgcolor: 'bg-steelblue',
									text: '去评价',
									link: '/pages/form/pm-work-eva/pm-work-eva?title=工作项评价&code=PER0302&itemid=' +
										item.id
								})
							}
							if (item.dutyUserName == this.userName) {
								if (item.planStart1 && nowDataStr >= new Date(item.planStart1).getTime()) {
									buttons.push({
										color: 'white',
										bgcolor: 'bg-steelblue',
										text: '去报告',
										link: '/pages/form/pm-work-report/pm-work-report?title=工作完成报告&code=PER0301&itemid=' +
											item.id
									})
									buttons.push({
										color: 'white',
										bgcolor: 'bg-steelblue',
										text: '去调整',
										link: '/pages/form/obj-work-monthlyworkadjustapply/obj-work-monthlyworkadjustapply?title=工作计划调整&code=OBJ0510&itemid=' +
											item.id
									})
								}
								if (!item.planStart1) {
									buttons.push({
										color: 'white',
										bgcolor: 'bg-steelblue',
										text: '去报告',
										link: '/pages/form/pm-work-report/pm-work-report?title=工作完成报告&code=PER0301&itemid=' +
											item.id
									})
									buttons.push({
										color: 'white',
										bgcolor: 'bg-steelblue',
										text: '去调整',
										link: '/pages/form/obj-work-monthlyworkadjustapply/obj-work-monthlyworkadjustapply?title=工作计划调整&code=OBJ0510&itemid=' +
											item.id
									})
								}
							}
						}
					}

					this.om.getPageInfo(item.showColumns, title, {
						buttons: JSON.stringify(buttons)
					}, item.id);
				}
			},
			loadData: function(func, tabOrder) {
				let _that = this;
				if ((_that['tab' + tabOrder].datas.length >= _that['tab' + tabOrder].total) && _that['tab' + tabOrder]
					.total != 0) {
					_that['tab' + tabOrder].pullUpOn = false;
					_that['tab' + tabOrder].loadding = false;
					return new Promise(resolve => {
						resolve();
					});
				}
				return _that['get' + func + 'List']()
					.then(res => {
						if (Object.prototype.toString.call(res.rows) === '[object Array]') {
							var datas = res.rows
							_that['tab' + tabOrder].total = res.total
							if (_that['tab' + tabOrder].datas.length && datas.length) {
								_that['tab' + tabOrder].datas = [..._that['tab' + tabOrder].datas, ...datas];
							} else if (!_that['tab' + tabOrder].datas.length && datas.length) {
								_that['tab' + tabOrder].datas = datas;
							}
							if (_that['tab' + tabOrder].datas.length < _that['tab' + tabOrder].total) {
								_that['tab' + tabOrder].page = _that['tab' + tabOrder].page + 1;
							} else {
								_that['tab' + tabOrder].pullUpOn = false;
							}
							return new Promise(resolve => {
								resolve();
							});
						} else {
							_that.om.toast(res.msg)
						}

					});
			},
			verifyStop(func) {
				if (func == 'Launch' && this.currentTab == 1) {
					return true
				} else if (func == 'Pending' && this.currentTab == 0) {
					return true
				} else {
					return false
				}
			},
			getLaunchList() {
				return this.om.request('tkeyWorkEvaluate/getNotCompleteWork', {
					q: this['tab' + this.currentTab].searchStr,
					page: this['tab' + this.currentTab].page + 1,
					rows: app.globalData.pageNum
				});
			},
			getPendingList() {
				return this.om.request('tkeyWorkEvaluate/getNotCompleteWork', {
					q: this['tab' + this.currentTab].searchStr,
					page: this['tab' + this.currentTab].page + 1,
					rows: app.globalData.pageNum
				});
			},
			btnChange0(index) {
				if (index == 0) {
					//报告单
					uni.navigateTo({
						url: "/pages/form/pm-work-report/pm-work-report?title=工作完成报告&code=PER0301"
					})
				} else if (index == 1) {
					//改期
					uni.navigateTo({
						url: "/pages/form/obj-work-monthlyworkadjustapply/new-work-monthlyworkadjustapply?title=工作计划调整&code=OBJ0510"
					})
				}
			},
			btnChange1(index) {
				//评价表
				uni.navigateTo({
					url: "/pages/form/pm-work-eva/pm-work-eva?title=工作项评价&code=PER0302"
				})
			}
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			this.pageTitle = options.title || '近期工作'
			uni.setNavigationBarTitle({
				title: this.pageTitle
			});
			// let _that = this
			// this.tab0.loadding = true;
			// // this.tab1.loadding = true;
			// _that.loadData('Launch',0).then(res=>{
			// 	this.tab0.loadding = false;
			// });
			// // _that.loadData('Pending',1).then(res=>{
			// // 	this.tab1.loadding = false;
			// // });
		},
		onShow() {
			let _that = this
			this.tab0.loadding = true;
			// this.tab1.loadding = true;
			_that.loadData('Launch', 0).then(res => {
				this.tab0.loadding = false;
			});
			// _that.loadData('Pending',1).then(res=>{
			// 	this.tab1.loadding = false;
			// });
		},
		onPageScroll(e) {
			let _that = this;
			_that['tab' + this.currentTab].scrollTop = e.scrollTop;
			let buttons = _that.buttons;
			if (e.scrollTop > 5) {
				if (_that.buttons.length === 1) {
					buttons.unshift(app.globalData.button.top);
					_that.buttons = buttons;
				}
			} else {
				if (_that.buttons.length === 2) {
					buttons.shift();
					_that.buttons = buttons;
				}
			}
		},
		//页面相关事件处理函数--监听用户下拉动作
		onPullDownRefresh: function() {
			let _that = this;
			_that.reloadData().then(res => {
				uni.stopPullDownRefresh()
				_that.om.toast('刷新成功')
			}, err => {
				uni.stopPullDownRefresh()
			});
		},

		// 页面上拉触底事件的处理函数
		onReachBottom: function() {
			if (!this['tab' + this.currentTab].pullUpOn) return;
			this['tab' + this.currentTab].loadding = true;
			let func = ""
			if (this.currentTab == 0) {
				func = 'Launch';
			} else {
				func = 'Pending';
			}
			let _that = this
			this.loadData(func, this.currentTab).then(res => {
				_that['tab' + _that.currentTab].loadding = false;
			});
		}
	};
</script>